import java.util.Stack;

public class MinStack {
    //需要两个栈，一个存储元素，一个存储压入的最小的元素
    private Stack<Integer> stack;
    private Stack<Integer> minStack;
    public MinStack() {
          stack = new Stack<>();
          minStack = new Stack<>();
    }

    public void push(int val) {
         //最小栈为空，需要将压入同步压入最小栈
        stack.push(val);
        if(minStack.isEmpty()) {
            minStack.push(val);
        }else{
            //最小栈不为空，判断最小栈的栈顶元素与当前元素的大小关心，如果最小站的栈顶元素大于当前元素，则将该元素压入最小栈
            if(val <= minStack.peek()) {
                minStack.push(val);
            }
        }
    }

    public void pop() {
        //判断栈的栈顶元素和最小栈的栈顶元素是否相等
        //Integer之间的比较要用equals
        if(stack.peek().equals(minStack.peek()) ) {
            //若相等，则同时弹栈
            stack.pop();
            minStack.pop();
        }else {
            stack.pop();
        }

    }

    public int top() {
        return stack.peek();
    }

    public int getMin() {
          return minStack.peek();
    }

    public static void main(String[] args) {
        MinStack minStack = new MinStack();
        minStack.push(512);
        minStack.push(-1024);
        minStack.push(-1024);
        minStack.push(512);
        minStack.pop();
        System.out.println(minStack.getMin());
        minStack.pop();
        System.out.println(minStack.getMin());
        minStack.pop();
        System.out.println(minStack.getMin());
    }
}
